package com.wawei.seeds.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.wawei.seeds.R;
import com.wawei.seeds.adapter.SeedsBaseAdapter;
import com.wawei.seeds.model.HeroInfo;
import com.wawei.seeds.model.WalnutMainEvent;
import com.wawei.seeds.services.ServiceHandler;
import com.wawei.seeds.services.WalnutPortraitServiceHandler;
import com.wawei.seeds.utils.ImageLoader;
import com.wawei.seeds.utils.RetrofitNetworkService;
import com.wawei.seeds.utils.WalnutBaseNofityHandler;

import java.util.List;

import butterknife.InjectView;
import retrofit.RestAdapter;

/**
 *
 * Created by t27891 on 15/7/2.
 */
public class MeSelfPortraitFragment extends MeBaseFragment {
    @InjectView(R.id.walnut_hero_head)ImageView headImage;
    @InjectView(R.id.item_self_portrait_name)TextView meAlias;
    @InjectView(R.id.item_self_portrait_gender)TextView meGender;

    private MeSelfPortraitNotifyHandler notifyHandler;
    private WalnutPortraitServiceHandler serviceHandler;
    private ImageLoader imageLoader;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        notifyHandler = new MeSelfPortraitNotifyHandler(this);
        serviceHandler = new WalnutPortraitServiceHandler();
        imageLoader = new ImageLoader(getActivity(), R.drawable.walnut_logo);
    }

    @Override
    public View onCreateViewBeforeInjection(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_walnut_myself, container, false);
    }

    @Override
    public void onCreateViewAfterInjection(View view, LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    }

    @Override
    public void onViewCreatedAfterTitle(View view, Bundle savedInstanceState) {
        RestAdapter restAdapter = RetrofitNetworkService.makeAsyncRestAdapter();
        Bundle bundle = new Bundle();
        bundle.putInt(WalnutPortraitServiceHandler.PORTRAIT_WHO, WalnutPortraitServiceHandler.PORTRAIT_SELF);
        serviceHandler.retrieveData(restAdapter, bundle, notifyHandler, WalnutBaseNofityHandler.INIT_ACHIEVE_DATA);
    }

    @Override
    public String getTitle() {
        return getString(R.string.walnut_me_personal_info);
    }

    @Override
    public ServiceHandler makeServiceHandler() {
        return null;
    }

    @Override
    public SeedsBaseAdapter<WalnutMainEvent> makeListAdapter() {
        return null;
    }

    @Override
    public Bundle makeConditionBundle(int phase) {
        return null;
    }

    @Override
    public Intent makeIntent4ListItemClicked(AdapterView<?> parent, View view, int position, long id, Object itemData) {
        return null;
    }

    private static class MeSelfPortraitNotifyHandler extends WalnutBaseNofityHandler<MeSelfPortraitFragment, HeroInfo>{
        public MeSelfPortraitNotifyHandler(MeSelfPortraitFragment meSelfPortraitFragment) {
            super(meSelfPortraitFragment);
        }

        @Override
        public void doInitData(MeSelfPortraitFragment meSelfPortraitFragment, List<HeroInfo> data) {
            if(null != data && data.size() > 0){
                HeroInfo hero = data.get(0);
                get().meAlias.setText(hero.alias);
                get().meGender.setText(hero.gender);
                get().imageLoader.loadImage(hero.headPortrait, get().headImage, true, false);
            }
        }
    }
}
